package com.pi.reservaCita2.controler;

import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;

import org.apache.tools.ant.taskdefs.Sleep;

import com.gargoylesoftware.htmlunit.AlertHandler;
import com.gargoylesoftware.htmlunit.util.Cookie;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.pi.reservaCita2.client.CitasView;
import com.pi.reservaCita2.client.Getdata;
import com.pi.reservaCita2.client.GetdataAsync;

public class FechBarHandler extends Composite implements ClickHandler {

	
	private DateBox dateBoxebegin ;
	private DateBox dateBoxeend;
	private Button search ;
	private TextBox txt;
	private CitasView citavi;
	private boolean control= false ; 
	private ArrayList<String[]> listadatosdb;
	VerticalPanel checkPanel;
	
	public boolean getDatacallback(String cadena)
	{
		 //boolean control;
		GetdataAsync data = GWT.create(Getdata.class);
		
		if(data==null)
		{data = GWT.create(Getdata.class);}
		
		data.getaDatadb(cadena, new AsyncCallback<ArrayList<String[]> > (){
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert("fallada es la consulta");
			}
			@Override
			public void onSuccess(ArrayList<String[]> result) {
				// TODO Auto-generated method stub 
			 	listadatosdb=result;
				setListadatosdb(listadatosdb);
				control=true;
				//control = true;
			 }});
		
		return control; 
		
	}
	public DateBox getDateBoxebegin() {
		return dateBoxebegin;
	}
	public void setDateBoxebegin(DateBox dateBoxebegin) {
		this.dateBoxebegin = dateBoxebegin;
	}
	public DateBox getDateBoxeend() {
		return dateBoxeend;
	}
	public void setDateBoxeend(DateBox dateBoxeend) {
		this.dateBoxeend = dateBoxeend;
	}
	public Button getSearch() {
		//search = new Button("Buscar Citas");
		//search.addClickHandler(this);
		return search;
	}
	public void setSearch(Button search) {
		this.search = search;
	}
	public VerticalPanel getCheckPanel() {
		checkPanel = new VerticalPanel();
		
		String [] week= {"Mañana","Tarde"};
		for(int i=0;i<week.length;i++)
		{
			CheckBox checkBox = new CheckBox(week[i]);
			checkBox.getElement().setId("Check" + week[i]);
			checkBox.ensureDebugId("Check" + week[i]);
			checkPanel.add(checkBox);
			}
		return  checkPanel;
	}
	public void setCheckPanel(VerticalPanel checkPanel) {
		this.checkPanel = checkPanel;
	}
	public FechBarHandler()
	{	txt = new TextBox();
		dateBoxebegin = new DateBox();		dateBoxebegin.getElement().setId("begin");
		dateBoxeend = new DateBox();		dateBoxeend.getElement().setId("end");
		
		//search = new Button("Buscar Citas");
		dateBoxebegin.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT)));
		
		dateBoxeend.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT)));
		
		listadatosdb = new ArrayList<String[]>();
		
		search = new Button("Buscar Citas");
		search.addClickHandler(this);
		
	}
	@Override
	public void onClick(ClickEvent event) {
		// TODO Auto-generated method stub
		Widget sender = (Widget) event.getSource();

	    if (sender == search) {
	    	
	    	if(dateBoxebegin.getValue()!=null && dateBoxeend.getValue()!=null)
	    	{
	    		
	    		if(dateBoxebegin.getValue().before(dateBoxeend.getValue()))
	    		{
	    			
	    			int especialidad = 1 ; // 
	    			int establecimiento = 4 ;  
	    			
	    	    	String dias= "(";
	    	    	String mese= "and mes in ( "+(dateBoxebegin.getValue().getMonth()+1)+" )";
	    	    	Set <Integer> diad= new TreeSet<Integer>();
	    	    	int lastvalue=0;
	    	    	if(dateBoxebegin.getValue().getMonth()<dateBoxeend.getValue().getMonth())
	    	    	{
	    	    		mese="and mes in (";
	    	    		for(int i=dateBoxebegin.getValue().getMonth()+1;i<=dateBoxeend.getValue().getMonth()+1;i++)
	    	    		{mese+=i + ",";}
	    	    		lastvalue=31;
	    	    		mese=mese.substring(0,mese.length()-1)+")";
	    	    	}
	    	    	
	    	    	for(int u =dateBoxebegin.getValue().getDate();u<=lastvalue+dateBoxeend.getValue().getDate();u++)
	    	    	{  diad.add(u%32); }
	    	    	
	    	    	for(Integer as : diad){dias+=as+",";}
	    	    	System.out.println(mese);
	    	    	
	    	    	dias=dias.substring(0,dias.length()-1)+")";
	    	    	System.out.println(dias);
	    	    	
	    	    	
	    	    	
	    	    	String query = "SELECT horario.\"idhorario\", horario.\"idagenda\"," +
	    	    	"agenda.idconsultorio, consultorio.\"nombreconsultorio\", " +
	    	    	"horario.\"horahorario\", horario.\"minutohorario\","+
	    	    	"agenda.\"anio\",agenda.\"mes\",agenda.\"dia\" "+
	    	    	"FROM consultorio INNER JOIN agenda on consultorio.idconsultorio"+
	    	    	 "= agenda.idconsultorio inner join horario on agenda.\"idagenda\" = horario.\"idagenda\" and "+  
	    	    	 "horario.idhorario in ("+
	    	    	"SELECT horario.idhorario from horario WHERE  horario.\"idagenda\"  in (SELECT \"idagenda\"  FROM agenda where \"idconsultorio\" in "+
	    	    	   "(select \"idconsultorio\" from consultorio where \"idespecialidad\" = 8 and \"idestablecimientosalud\" = 1)"+
	    	    		"and dia in "+dias +"" +mese+") group by horario.\"idagenda\" , horario.\"idhorario\" , agenda.\"dia\")";
	    	    	
	    	    	
	    	    	this.getDatacallback(query);
	    	    	
	    	    	txt.setText(listadatosdb.size()+""+ " monga te kiero pero ....");
	    	    	citavi= new CitasView(1.5,Unit.EM);
	    	    	citavi.DrawTable(listadatosdb);
	    	    	RootPanel.get("citaPanel").clear();
	    	    	RootPanel.get("citaPanel").add(citavi);
	    	    		
	    	    	
	    	    
	    	    	
	    				
	    			
	    	    	
	    		}else{
	    			Window.alert("no permitido");
	    		}
	    		
	    			
	    		
	    	}
	    	else
	    	{
	    		Window.alert("tienen que llenar los campos");
	    		}
	    	
	    	   	
	    	//
	    	//CheckBox lsl= new CheckBox();
	    	//lsl.isChecked();
	    	//boolean dia=;
	    	//boolean tarde=; 
	    	
	    	/*InputElement checkbox = DOM.getElementById("gwt-uid-10").cast();
	    	if (!checkbox.isChecked()) {
	    	  //checkbox.setChecked(true);
	    	}else{Window.alert("falta check !!");}
	    	*/
	      // handle b1 being clicked
	    	//AlertHandler("hola");
	    }
	}
	public TextBox getTxt() {
		return txt;
	}
	public void setTxt(TextBox txt) {
		this.txt = txt;
	}
	public ArrayList<String[]> getListadatosdb() {
		return listadatosdb;
	}
	public void setListadatosdb(ArrayList<String[]> listadatosdb) {
		this.listadatosdb = listadatosdb;
	}

}
